<template>
	<view>
 	<view class="addressBtn" >
		  <button v-if="!addressInfo" @click="chooseAdress" type="primary" size="mini">请选择收获地址+</button>
		 <view class="addressInfo" @click="chooseAdress" v-else>
		    <view class="info_one">
		         <text>收货人：{{addressInfo.userName}}</text>
		          <text>电话：{{addressInfo.telNumber}} ></text>
		    </view>
        <view class="info_two">
          收获地址:{{addressInfo.provinceName}}
        </view>
		  </view>
		  </view>
	</view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
	export default {
		data() {
			return {
		
			};
		},
    computed:{
      ...mapState('m_cart',['addressInfo'])
    },
    methods:{
      ...mapMutations('m_cart',['changeAddress']),
    async  chooseAdress(){
      wx.getSetting
      const [err,sucess]=await uni.chooseAddress().catch(err=>err)
      if(err==null&&sucess.errMsg=="chooseAddress:ok"){
        if(sucess.provinceName!=''){
          sucess.provinceName=sucess.provinceName+sucess.cityName+sucess.countyName+sucess.detailInfo
        }
        this.addressInfo=sucess
        this.changeAddress(sucess)
        console.log(sucess)
      }
        // 3. 用户没有授权
        if (err && err.errMsg === 'chooseAddress:fail auth deny') {
          this.reAuth() // 调用 this.reAuth() 方法，向用户重新发起授权申请
        }
      },
    async  reAuth(){
        const [err,res]=await uni.showModal({
          content:'检测到您没打开地址权限，是否去设置打开？',
          confirmText:'确认',
          cancelText:'取消'
        })
        if(err) return uni.$showMsg('您取消了地址授权')
        if(res.confirm) return uni.openSetting({
          success:(res)=>{
            if(!res.authSetting['scope.address']) return uni.$showMsg('您取消了授权')
            if(res.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择收获地址')
          }
        })
      }
    }
	}
</script>

<style lang="scss">
  .addressBtn{
    display: flex;
    justify-content: center;
    align-items: center;
     font-size: 12px;
    padding: 20px 4px;
    border-bottom: 3px solid #1aad19;
    .info_one{
      width: 750rpx;
      display: flex;
      justify-content: space-between;
    }
    .info_two{
      width: 750rpx;
      padding: 5px 0;
    }
  }

</style>
